/*
* =====================================================================================
*
*       Filename:  main.cpp
*
*    Description: Minimalistic project example that uses both Qt and OpenSceneGraph libraries.
*
*        Version:  1.0
*        Created:  30-Jun-16 10:23:06 AM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Victoria Rudakova (vicrucann@gmail.com),
*   Organization:  vicrucann.github.io
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#include "QtOSGWidget.h"

#include <QApplication>
#include <QMainWindow>
//#include <QOpenGLWidget>
//#include <QMouseEvent>
//#include <QWheelEvent>
#include <QDesktopWidget>
//#include <QScreen>
//#include <QtGlobal>
//#include <QWindow>
//
//#include <osg/ref_ptr>
//#include <osgViewer/GraphicsWindow>
//#include <osgViewer/Viewer>
//#include <osg/Camera>
//#include <osg/ShapeDrawable>
//#include <osg/StateSet>
//#include <osg/Material>
//#include <osgGA/EventQueue>
//#include <osgGA/TrackballManipulator>
//#include <osgViewer/ViewerEventHandlers>
//#include <osgDB/ReadFile>

//
//class QtOSGWidget : public QOpenGLWidget
//{
//public:
//	QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent = 0)
//		: QOpenGLWidget(parent)
//		, _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded(this->x(), this->y(),
//			this->width(), this->height()))
//		, _mViewer(new osgViewer::Viewer)
//		, m_scaleX(scaleX)
//		, m_scaleY(scaleY)
//	{
//		osg::Camera* camera = new osg::Camera;
//		camera->setViewport(0, 0, this->width(), this->height());
//		camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
//		float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
//		camera->setProjectionMatrixAsPerspective(30.f, aspectRatio, 1.f, 1000.f);
//		camera->setGraphicsContext(_mGraphicsWindow);
//
//		_mViewer->setCamera(camera);
//		_mViewer->setSceneData(osgDB::readNodeFile("F:/Other/model/20160908.ive"));
//		_mViewer->addEventHandler(new osgViewer::StatsHandler);
//
//		osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
//		manipulator->setAllowThrow(false);
//		_mViewer->setCameraManipulator(manipulator);
//
//		_mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
//		_mViewer->realize();
//
//		// This ensures that the widget will receive keyboard events. This focus
//		// policy is not set by default. The default, Qt::NoFocus, will result in
//		// keyboard events that are ignored.
//		this->setFocusPolicy(Qt::StrongFocus);
//		this->setMinimumSize(100, 100);
//
//		// Ensures that the widget receives mouse move events even though no
//		// mouse button has been pressed. We require this in order to let the
//		// graphics window switch viewports properly.
//		this->setMouseTracking(true);
//	}
//
//
//	virtual ~QtOSGWidget() {}
//
//	void setScale(qreal X, qreal Y)
//	{
//		m_scaleX = X;
//		m_scaleY = Y;
//		this->resizeGL(this->width(), this->height());
//	}
//
//protected:
//
//	void keyPressEvent(QKeyEvent* event)
//	{
//		QString keyString = event->text();
//		const char* keyData = keyString.toLocal8Bit().data();
//
//		if (event->key() == Qt::Key_S)
//		{
//#ifdef WITH_SELECTION_PROCESSING
//			selectionActive_ = !selectionActive_;
//#endif
//
//			// Further processing is required for the statistics handler here, so we do
//			// not return right away.
//		}
//		else if (event->key() == Qt::Key_H)
//		{
//			this->onHome();
//			return;
//		}
//
//		this->getEventQueue()->keyPress(osgGA::GUIEventAdapter::KeySymbol(*keyData));
//	}
//
//	void keyReleaseEvent(QKeyEvent* event)
//	{
//		QString keyString = event->text();
//		const char* keyData = keyString.toLocal8Bit().data();
//
//		this->getEventQueue()->keyRelease(osgGA::GUIEventAdapter::KeySymbol(*keyData));
//	}
//
//	void onHome()
//	{
//		_mViewer->home();
//	}
//
//	void setKeyboardModifiers(QInputEvent* event)
//	{
//		int modkey = event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier);
//		unsigned int mask = 0;
//		if (modkey & Qt::ShiftModifier) mask |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
//		if (modkey & Qt::ControlModifier) mask |= osgGA::GUIEventAdapter::MODKEY_CTRL;
//		if (modkey & Qt::AltModifier) mask |= osgGA::GUIEventAdapter::MODKEY_ALT;
//		this->getEventQueue()->getCurrentEventState()->setModKeyMask(mask);
//	}
//
//	virtual void paintGL() {
//		_mViewer->frame();
//	}
//
//	virtual void resizeGL(int width, int height)
//	{
//		this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
//		_mGraphicsWindow->resized(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
//		osg::Camera* camera = _mViewer->getCamera();
//		camera->setViewport(0, 0, this->width()*m_scaleX, this->height()* m_scaleY);
//	}
//
//	virtual void initializeGL() {
//		osg::Node* geode = _mViewer->getSceneData();
//		osg::StateSet* stateSet = geode->getOrCreateStateSet();
//		osg::Material* material = new osg::Material;
//		material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
//		stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
//		stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
//	}
//
//	virtual void mouseMoveEvent(QMouseEvent* event)
//	{
//		this->getEventQueue()->mouseMotion(event->x()*m_scaleX, event->y()*m_scaleY);
//	}
//
//	virtual void mousePressEvent(QMouseEvent* event)
//	{
//		unsigned int button = 0;
//		switch (event->button()) {
//		case Qt::LeftButton:
//			button = 1;
//			break;
//		case Qt::MiddleButton:
//			button = 2;
//			break;
//		case Qt::RightButton:
//			button = 3;
//			break;
//		default:
//			break;
//		}
//		this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button);
//	}
//
//	virtual void mouseReleaseEvent(QMouseEvent* event)
//	{
//		unsigned int button = 0;
//		switch (event->button()) {
//		case Qt::LeftButton:
//			button = 1;
//			break;
//		case Qt::MiddleButton:
//			button = 2;
//			break;
//		case Qt::RightButton:
//			button = 3;
//			break;
//		default:
//			break;
//		}
//		this->getEventQueue()->mouseButtonRelease(event->x()*m_scaleX, event->y()*m_scaleY, button);
//	}
//
//	virtual void wheelEvent(QWheelEvent* event)
//	{
//		int delta = event->delta();
//		osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ?
//			osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;
//		this->getEventQueue()->mouseScroll(motion);
//	}
//
//	virtual bool event(QEvent* event)
//	{
//		bool handled = QOpenGLWidget::event(event);
//		this->update();
//		return handled;
//	}
//
//private:
//
//	osgGA::EventQueue* getEventQueue() const {
//		osgGA::EventQueue* eventQueue = _mGraphicsWindow->getEventQueue();
//		return eventQueue;
//	}
//
//	osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _mGraphicsWindow;
//	osg::ref_ptr<osgViewer::Viewer> _mViewer;
//	qreal m_scaleX, m_scaleY;
//};

int main(int argc, char** argv)
{
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
	QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
#else
	qputenv("QT_DEVICE_PIXEL_RATIO", QByteArray("1"));
#endif

	QApplication qapp(argc, argv);

	int x = QApplication::desktop()->physicalDpiX();
	int y = QApplication::desktop()->physicalDpiY();
	double scaleX = x / double(x);
	double scaleY = y / double(y);

	QMainWindow window;
	YY::QtOSGWidget* widget = new YY::QtOSGWidget(scaleX, scaleY, &window);
	window.setCentralWidget(widget);
	window.resize(1024, 768);
	widget->getViewer()->setSceneData(osgDB::readNodeFile("F:/Other/model/20160908.ive"));
	//widget->getViewer()->setSceneData(osgDB::readNodeFile("cow.osg"));
	widget->getViewer();
	window.show();

	return qapp.exec();
}
